package nero.yncc.com.createart.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.activity.ActivitySearch;
import nero.yncc.com.createart.activity.ActivitySearchAllGod;
import nero.yncc.com.createart.adapter.AdapterCategoryLeftBox;
import nero.yncc.com.createart.adapter.AdapterCategoryRightBox;
import nero.yncc.com.createart.base.BaseFragment;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.BeanCategory;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.utils.HttpUtils;
import nero.yncc.com.createart.utils.ImageUtils;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * 分类页面
 * Created by Administrator.
 * Time:2017/3/10 09:38.
 */

@ContentView(R.layout.fragment_category)
public class FragmentCategory extends BaseFragment {
    @ViewInject(R.id.lv_left)
    ListView lvLeft;

    @ViewInject(R.id.rl_list)
    PullToRefreshGridView rl_list;

    @ViewInject(R.id.tv_right_title)
    TextView tv_right_title;
    @ViewInject(R.id.iv_image)
    ImageView iv_image;
    @ViewInject(R.id.et_titlebar)
    TextView et_titlebar;
    @ViewInject(R.id.icon_nodata)
    IconView icon_nodata;

    private ArrayList<BeanCategory> listLeft = new ArrayList<>();
    ArrayList<BeanCategory> listRight = new ArrayList<>();
    private AdapterCategoryLeftBox lAdapter;
    private AdapterCategoryRightBox rAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = x.view().inject(this, inflater, container);
        initAll();
        return view;
    }

    @Override
    protected void initView() {
        ding();//顶出状态栏

        lAdapter = new AdapterCategoryLeftBox(listLeft, getActivity());
        rAdapter = new AdapterCategoryRightBox(listRight, getActivity());
        lvLeft.setAdapter(lAdapter);

        rl_list.setAdapter(rAdapter);
        rl_list.getRefreshableView().setNumColumns(3);
        //自动顶出状态栏

        getStateBarHeight();

    }

    @Override
    protected void getHttpData() {
        super.getHttpData();
        getData();
    }

    String id = "0";

    @Override
    protected void initData() {

        lAdapter.setSelectIndex(0);

        lvLeft.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                icon_nodata.setVisibility(View.GONE);
                rl_list.setVisibility(VISIBLE);
                tv_right_title.setText(listLeft.get((int) l).getName());
                ImageUtils.BindImage(getContext(), iv_image, listLeft.get((int) l).getImage(), 4,4);
                lAdapter.setSelectIndex(i);
                lAdapter.notifyDataSetChanged();
                listRight.clear();
                rAdapter.notifyDataSetChanged();
                id = listLeft.get((int) l).getId();

                rl_list.setRefreshing(true);
            }
        });


        lAdapter.notifyDataSetChanged();


        rAdapter.notifyDataSetChanged();
    }


    @Override
    public void getData() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("category_id", id);//获取顶级分类

        HttpUtils.get(getActivity(), Constants.API_CATEGORY, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                try {
                    JSONObject jo = new JSONObject(json.toString());
                    JSONArray jsonArray = jo.getJSONArray("list");
                    if(listRight.size()<=0){
//                        rl_list.setVisibility(GONE);
                        icon_nodata.setVisibility(VISIBLE);
                    }else{
//                        rl_list.setVisibility(VISIBLE);
                        icon_nodata.setVisibility(GONE);
                    }


                    if (id.equals("0")) {
                        listLeft.clear();
                        lAdapter.notifyDataSetChanged();
                        for (int a = 0; a < jsonArray.length(); a++) {
//                        if(type.equals("0")){
                            listLeft.add(JSON.parseObject(jsonArray.getString(a), BeanCategory.class));
//                        }else{
//                            rAdapter.addCategory(0,JSON.parseObject(jsonArray.getString(a), BeanCategory.class));
//                        }
                        }
                    } else {
                        listRight.clear();
                        rAdapter.notifyDataSetChanged();
                        for (int a = 0; a < jsonArray.length(); a++) {
//                        if(type.equals("0")){
                            listRight.add(JSON.parseObject(jsonArray.getString(a), BeanCategory.class));
//                        }else{
//                            rAdapter.addCategory(0,JSON.parseObject(jsonArray.getString(a), BeanCategory.class));
//                        }
                        }
                    }


                    if (listLeft.size() > 0 && id.equals("0")) {
                        id = listLeft.get(0).getId();
                        rl_list.setRefreshing(true);
                        tv_right_title.setText(listLeft.get(0).getName());
                        ImageUtils.BindImage(getContext(), iv_image, listLeft.get(0).getImage(),4);
                    }

                    lAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    rl_list.onRefreshComplete();
                }


            }

            @Override
            public void fail(String failMsg) {
                rl_list.onRefreshComplete();
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        et_titlebar.setOnClickListener(this);
//        et_titlebar.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
//                if (i == EditorInfo.IME_ACTION_SEARCH) {
//                    String keyword = et_titlebar.getText().toString();
//                    Bundle bundle = new Bundle();
//                    bundle.putString("keyword", keyword);
//                    openActivity(ActivitySearchAllGod.class, bundle);
//                }
//                return false;
//            }
//        });
        rl_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {
            @Override
            public void onRefresh(PullToRefreshBase<GridView> refreshView) {
                listRight.clear();
                getData();
            }
        });
    }

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        switch (id){
            case R.id.et_titlebar:
                openActivity(ActivitySearch.class,null);
                break;
        }
    }
}
